import java.util.LinkedList;

public class WaitNotifyStorage {
    public static final int MAX_SIZE = 100;

    private LinkedList<Object> list = new LinkedList<>();

    public void produce(String product) {
        synchronized (list) {
            while (list.size() == MAX_SIZE) {
                System.out.println("仓库已满");
                try {
                    list.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            list.add(product);
            System.out.println("仓库已满");
            list.notifyAll();
        }
    }

    public Object consume() {
        Object res = null;
        synchronized (list) {
            while (list.size() == 0) {
                System.out.println("仓库已空");
                try {
                    list.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            res = list.remove();
            list.notifyAll();
        }
        return res;
    }
}
